/*
 * Garden.cpp
 *
 *  Created on: 01.11.2010
 *      Author: 123
 */

#include "Garden.h"

void Garden::MoveAndEatAnimals(animals *a) {
	int i;

	for (i = 0; i < AnimalAmount; i++) {

		if ((a->NumberCage == animal[i].NumberCage) && CollisionDetection(
				a->x - animalinfo[a->type - 1].xeat,
				(a->y - animalinfo[a->type - 1].yeat),
				(animalinfo[a->type - 1].xeat * 2),
				(animalinfo[a->type - 1].yeat * 2),
				(animal[i].x - animalinfo[animal[i].type - 1].xeat),
				(animal[i].y - animalinfo[animal[i].type - 1].yeat),
				(animalinfo[animal[i].type - 1].xeat * 2),
				(animalinfo[animal[i].type - 1].yeat * 2)) && animal[i].type
				< a->type && animal[i].type > -1) {
			animal[i].type = -1;
			if (animal[i].AmountEat != 0)
				a->AmountEat += animal[i].AmountEat + 1;
			else
				a->AmountEat += 1;
			if (a->AttackAnimal)
				a->AttackAnimal = 0;
			if (animal[i].IsAttackedByAnimal)
				animal[i].IsAttackedByAnimal = 0;
		}

		if (animal[i].type < a->type && ((!animal[i].IsAttackedByAnimal
				and !a->AttackAnimal) || (animal[i].IsAttackedByAnimal
				&& a->AttackAnimal)) && animal[i].type > -1 && (abs(
				a->x - animal[i].x) < 50) && (abs(a->y - animal[i].y) < 50)
				&& (a->NumberCage == animal[i].NumberCage)) {

			a->AttackAnimal = 1;
			animal[i].IsAttackedByAnimal = 1;

			if (a->x - animalinfo[a->type - 1].xeat > animal[i].x - animalinfo[
					animal[i].type - 1].xeat && a->x
					- animalinfo[a->type - 1].xeat
					> CageXY[animal[i].NumberCage][0]) {
				a->x -= animalinfo[a->type - 1].speed;
				if (animal[i].x - animalinfo[animal[i].type - 1].xeat
						> CageXY[animal[i].NumberCage][0])
					animal[i].x -= animalinfo[animal[i].type - 1].speed;
			}
			if (a->y - animalinfo[a->type - 1].yeat > animal[i].y - animalinfo[
					animal[i].type - 1].yeat && a->y
					- animalinfo[a->type - 1].yeat > CageXY[0][1]) {
				a->y -= animalinfo[a->type - 1].speed;
				if (animal[i].y - animalinfo[animal[i].type - 1].yeat
						> CageXY[0][1])
					animal[i].y -= animalinfo[animal[i].type - 1].speed;
			}
			if (a->x + animalinfo[a->type - 1].xeat < animal[i].x + animalinfo[
					animal[i].type - 1].xeat && a->x
					+ animalinfo[a->type - 1].xeat
					< CageXY[a->NumberCage + 1][0]) {
				a->x += animalinfo[a->type - 1].speed;
				if (animal[i].x + animalinfo[animal[i].type - 1].xeat < CageXY[
						animal[i].NumberCage + 1][0])
					animal[i].x += animalinfo[animal[i].type - 1].speed;
			}
			if (a->y + animalinfo[a->type - 1].yeat < animal[i].y + animalinfo[
					animal[i].type - 1].yeat && a->y
					+ animalinfo[a->type - 1].yeat
					< CageXY[a->NumberCage + 1][1]) {
				a->y += animalinfo[a->type - 1].speed;
				if (animal[i].y + animalinfo[animal[i].type - 1].yeat < CageXY[
						animal[i].NumberCage + 1][1])
					animal[i].y += animalinfo[animal[i].type - 1].speed;
			}
		}

		if ((a->NumberCage == animal[i].NumberCage) && (
				animal[i].IsAttackedByAnimal && a->AttackAnimal)) {
			animal[i].IsAttackedByAnimal = 0;
			a->AttackAnimal = 0;
		}

	}
	if (a->AmountEat < animalinfo[a->type - 1].maxLevelAmount && !a->twise && (
			a->AmountEat > 7 && a->level == 0)) {
		a->level += 1;
		a->strength = a->level;
		a->twise = 1;
	}

	if (!a->IsAttackedByAnimal && !a->AttackAnimal) {
		if (!a->directiongo) {
			a->direction = rand() % 4 + 1;
			a->timedirection = 0;
			a->directiongo = true;
		} else {
			a->timedirection++;
			if (a->timedirection < 30) {
				if (a->x - animalinfo[a->type - 1].xeat
						< CageXY[a->NumberCage][0] && ChangeAnimalNumber != i &&
						a->x + animalinfo[a->type - 1].xeat < 480) {
					a->direction = rand() % 4 + 1;
					a->x = CageXY[a->NumberCage][0]
							+ animalinfo[a->type - 1].xeat;
				}

				if (a->x + animalinfo[a->type - 1].xeat > CageXY[a->NumberCage
						+ 1][0] && ChangeAnimalNumber != i && a->x
						+ animalinfo[a->type - 1].xeat > 0) {
					a->direction = rand() % 4 + 1;
					a->x = CageXY[a->NumberCage + 1][0] - animalinfo[a->type
							- 1].xeat;
				}

				if (a->y - animalinfo[a->type - 1].yeat < CageXY[0][1]) {
					a->direction = rand() % 4 + 1;
					a->y = CageXY[0][1] + animalinfo[a->type - 1].yeat;
				}
				if (a->y + animalinfo[a->type - 1].yeat > CageXY[a->NumberCage
						+ 1][1]) {
					a->direction = rand() % 4 + 1;
					a->y = CageXY[a->NumberCage + 1][1] - animalinfo[a->type
							- 1].yeat;
				}
				switch (a->direction) {
				case right:
					a->x += animalinfo[a->type - 1].speed;
					break;
				case up:
					a->y -= animalinfo[a->type - 1].speed;
					break;
				case left:
					a->x -= animalinfo[a->type - 1].speed;
					break;
				case down:
					a->y += animalinfo[a->type - 1].speed;
					break;
				}

			} else {

				a->directiongo = false;
			}
		}
	}
}

void Garden::Think() {
	//ChangeAnimalNumber=-1;
	if (LineX < 0)
		LineX = 0;
	if (LineX > 460)
		LineX = 460;
	if (LineY < 0)
		LineY = 0;
	if (LineY > 252)
		LineY = 252;

	for (i = 32; i <= 120; i += 48) {
		if (osl_keys->analogX > i) {
			LineX++;
		}
		if (osl_keys->analogX < -i) {
			LineX--;
		}
		if (osl_keys->analogY > i) {
			LineY++;
		}
		if (osl_keys->analogY < -i) {
			LineY--;
		}
	}

	for (i = 0; i < 16; i++) {
		for (j = 0; j < 17; j++)
			oslDrawImageXY(TileTex[TileRand[i][j]],i*32,j*16);
	}
	for (i = 0; i < AnimalAmount; i++) {
		if (!HeroCatchAnAnimal) {
			if (CollisionDetection(LineX, LineY, 20, 20,
					animal[i].x - animalinfo[animal[i].type - 1].xeat,
					animal[i].y - animalinfo[animal[i].type - 1].yeat,
					animalinfo[animal[i].type - 1].xeat * 2,
					animalinfo[animal[i].type - 1].yeat * 2) && animal[i].type
					!= -1) {
				ChangeAnimalNumber = i;
			}
		}
		if (animal[i].type > -1) {

			oslDrawImageXY(AnimalTex[animal[i].type-1],(int)animal[i].x,(int)animal[i].y);
			MoveAndEatAnimals(&animal[i]);

			/*	pgeFontActivate(nash);
			 pgeFontPrintf(nash,animal[i].x,animal[i].y,RED,"%d",animal[i].AmountEat);*/

		}

	}
	if (osl_keys->released.cross && ChangeAnimalNumber != -1) {
		HeroCatchAnAnimal = false;
		ChangeAnimalNumber = -1;

	}
	if (osl_keys->pressed.cross && ChangeAnimalNumber != -1)
		HeroCatchAnAnimal = true;
	if (HeroCatchAnAnimal) {
		animal[ChangeAnimalNumber].x = LineX + 10;
		animal[ChangeAnimalNumber].y = LineY + 10;
		if (CageXY[animal[ChangeAnimalNumber].NumberCage][0]
				> animal[ChangeAnimalNumber].x) {
			animal[ChangeAnimalNumber].NumberCage -= 1;
		}
		if (CageXY[animal[ChangeAnimalNumber].NumberCage + 1][0]
				< animal[ChangeAnimalNumber].x) {
			animal[ChangeAnimalNumber].NumberCage += 1;
		}

	}

	for (i = 0; i < AnimalAmount; i++) {

		if (animal[i].type > -1) {
			pgeFontActivate(nash);
			pgeFontPrintf(nash, animal[i].x, animal[i].y, GRAY, "%d%d",
					animal[i].AmountEat, animal[i].level);
		}
	}
	oslDrawLine(LineX, LineY, LineX + 20, LineY + 20, RED);
	oslDrawLine(LineX, LineY + 20, LineX + 20, LineY, RED);
	oslDrawRect(LineX, LineY, LineX + 20, LineY + 20, RED);

	for (int i = 0; i < 3; i++)
		oslDrawLine(CageXY[i][0], 0, CageXY[i][0], CageXY[i][1], BLACK);

}
void Garden::Control() {
	if (LineX < 0)
		LineX = 0;
	if (LineX > 460)
		LineX = 460;
	if (LineY < 0)
		LineY = 0;
	if (LineY > 252)
		LineY = 252;
	for (i = 32; i <= 120; i += 48) {
		if (osl_keys->analogX > i) {
			LineX++;
		}
		if (osl_keys->analogX < -i) {
			LineX--;
		}
		if (osl_keys->analogY > i) {
			LineY++;
		}
		if (osl_keys->analogY < -i) {
			LineY--;
		}
	}
	for (i = 0; i < AnimalAmount; i++) {
		if (!HeroCatchAnAnimal) {
			if (CollisionDetection(LineX, LineY, 20, 20,
					animal[i].x - animalinfo[animal[i].type - 1].xeat,
					animal[i].y - animalinfo[animal[i].type - 1].yeat,
					animalinfo[animal[i].type - 1].xeat * 2,
					animalinfo[animal[i].type - 1].yeat * 2) && animal[i].type
					!= -1) {
				ChangeAnimalNumber = i;
			}
		}
		if (animal[i].type > -1) {

			oslDrawImageXY(AnimalTex[animal[i].type-1],(int)animal[i].x,(int)animal[i].y);
			MoveAndEatAnimals(&animal[i]);

			/*	pgeFontActivate(nash);
			 pgeFontPrintf(nash,animal[i].x,animal[i].y,RED,"%d",animal[i].AmountEat);*/

		}

	}

	if (osl_keys->pressed.cross && ChangeAnimalNumber != -1)
		HeroCatchAnAnimal = true;

	if (HeroCatchAnAnimal) {
		animal[ChangeAnimalNumber].x = LineX + 10;
		animal[ChangeAnimalNumber].y = LineY + 10;
		if (CageXY[animal[ChangeAnimalNumber].NumberCage][0]
				> animal[ChangeAnimalNumber].x) {
			animal[ChangeAnimalNumber].NumberCage -= 1;
		}
		if (CageXY[animal[ChangeAnimalNumber].NumberCage + 1][0]
				< animal[ChangeAnimalNumber].x) {
			animal[ChangeAnimalNumber].NumberCage += 1;
		}

	}
	if (osl_keys->released.cross && ChangeAnimalNumber != -1) {
		HeroCatchAnAnimal = false;
		ChangeAnimalNumber = -1;

	}
}
void Garden::Draw() {
	for (i = 0; i < AnimalAmount; i++) {

		if (animal[i].type > -1) {
			pgeFontActivate(nash);
			pgeFontPrintf(nash, animal[i].x, animal[i].y, GRAY, "%d%d",
					animal[i].AmountEat, animal[i].level);
		}
	}
	for (i = 0; i < 16; i++) {
		for (j = 0; j < 17; j++)
			oslDrawImageXY(TileTex[TileRand[i][j]],i*32,j*16);
	}

	oslDrawLine(LineX, LineY, LineX + 20, LineY + 20, RED);
	oslDrawLine(LineX, LineY + 20, LineX + 20, LineY, RED);
	oslDrawRect(LineX, LineY, LineX + 20, LineY + 20, RED);

	for (int i = 0; i < 3; i++)
		oslDrawLine(CageXY[i][0], 0, CageXY[i][0], CageXY[i][1], BLACK);
}
Garden AnimalGarden;

